<?php
namespace App\Admin\Controllers;

use App\Models\Category;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Tree;


class CategoriesController extends Controller
{
    //use HasResourceActions;
    use ModelForm;

    public function index(Content $content)
    {




        return $content
            ->header('商品类目列表')
//            ->body($this->grid());
            ->body(Category::tree());
    }

    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑商品类目')
            ->body($this->form(true)->edit($id));
    }

    public function create(Content $content)
    {
        return $content
            ->header('创建商品类目')
            ->body($this->form(false));
    }

    protected function grid()
    {
        $grid = new Grid(new Category);

        $grid->filter(function($filter){

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('name', '名称');

        });

        $grid->id('ID')->sortable();
        $grid->name('名称');
        $grid->level('层级');
        $grid->is_directory('是否目录')->display(function ($value) {
            return $value ? '是' : '否';
        });
        $grid->path('类目路径');
        $grid->disableExport();
        $grid->actions(function ($actions) {
            // 不展示 Laravel-Admin 默认的查看按钮
            $actions->disableView();
        });
        $grid->tools(function ($tools) {
            // 禁用批量删除按钮
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        $grid->actions(function ($actions) {
            // prepend一个操作
            $actions->prepend("<a href='/admin/users/downline/".$actions->getKey()."'><i class='fa fa-user-o'></i></a>");
            $actions->disableView();
            $actions->disableDelete();
        });


        return $grid;
    }

    protected function form($isEditing = false)
    {
        // Laravel-Admin 1.5.19 之后的新写法，原写法也仍然可用
        $form = new Form(new Category);

        $form->text('title', '类目名称')->rules('required');


        return $form;
    }

    // 定义下拉框搜索接口
    public function apiIndex(Request $request)
    {
        // 用户输入的值通过 q 参数获取
        $search = $request->input('q');

        $result = Category::query()
            // 通过 is_directory 参数来控制
            //->where('is_directory', boolval($request->input('is_directory', true)))
            ->where('title','like',"%".$search."%")
            //->where('parent_id',$search)
            ->paginate();


        // 把查询出来的结果重新组装成 Laravel-Admin 需要的格式
        $result->setCollection($result->getCollection()->map(function (Category $category) {
            return ['id' => $category->id, 'text' => $category->title];
        }));



        return $result;
    }
}